﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HR_ASP.ViewModel;
using DB.Model;

namespace HR_ASP.Controllers
{
    public class PracowBadanController : Controller
    {
        //
        // GET: /PracowBadan/

        public ActionResult Index()
        {
            using (var context = new HREntities())
            {
                return View(context.Pracow_Badan.Select(n => new PracowBadanView()
                {
                    ID = n.ID_PB,
                    IDBad = (int)n.ID_Bad,
                    IDPrac = (int)n.ID_Pracow,
                    DataOstatnBadania = (DateTime)n.Data_Ostatn_Badan,
                    DataWaznosci = (DateTime)n.Data_Wazn,
                    DataM = (DateTime)n.DataM,
                    DataU = (DateTime)n.DataU

                }).ToArray()
                );
            }
        }

        //
        // GET: /PracowBadan/Details/5

        public ActionResult Details(int id)
        {
            using (var context = new HREntities())
            {
                Pracow_Badan pracowBadanDB = context.Pracow_Badan.SingleOrDefault(n => n.ID_PB == id);
                if (pracowBadanDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new PracowBadanView(pracowBadanDB));
            }
        }

        //
        // GET: /PracowBadan/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /PracowBadan/Create

        [HttpPost]
        public ActionResult Create(PracowBadanView pracowBadan)
        {
            try
            {
                using (var context = new HREntities())
                {
                    context.Pracow_Badan.AddObject(pracowBadan.getModelDB(new Pracow_Badan()));
                    context.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /PracowBadan/Edit/5
 
        public ActionResult Edit(int id)
        {
            using (var context = new HREntities())
            {
                Pracow_Badan pracowBadanDB = context.Pracow_Badan.SingleOrDefault(n => n.ID_PB == id);
                if (pracowBadanDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new PracowBadanView(pracowBadanDB));
            }
        }

        //
        // POST: /PracowBadan/Edit/5

        [HttpPost]
        public ActionResult Edit(PracowBadanView pracowBadan)
        {
            try
            {
                using (var context = new HREntities())
                {
                    Pracow_Badan pracowBadanDB = context.Pracow_Badan.SingleOrDefault(n => n.ID_PB == pracowBadan.ID);
                    context.ApplyCurrentValues<Pracow_Badan>("Pracow_Badan", pracowBadan.getModelDB(pracowBadanDB));
                    context.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /PracowBadan/Delete/5
 
        public ActionResult Delete(int id)
        {
            using (var context = new HREntities())
            {
                Pracow_Badan pracowBadanDB = context.Pracow_Badan.SingleOrDefault(n => n.ID_PB == id);
                if (pracowBadanDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new PracowBadanView(pracowBadanDB));
            }
        }

        //
        // POST: /PracowBadan/Delete/5

        [HttpPost]
        public ActionResult Delete(PracowBadanView pracowBadan)
        {
            try
            {
                using (var context = new HREntities())
                {
                    Pracow_Badan pracowBadanDB = context.Pracow_Badan.SingleOrDefault(n => n.ID_PB == pracowBadan.ID);
                    if (pracowBadanDB != null)
                    {
                        context.Pracow_Badan.DeleteObject(pracowBadanDB);
                        context.SaveChanges();
                    }
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
